<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>printf - C++ Reference</title>
<style type="text/css">.hierarchy DIV {width:100px; height:24px; text-align:center; padding-top:8px; border:black 1px solid} </style>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
<link rel="stylesheet" type="text/css" href="/main29d.css">
<script src="/main29d.js" type="text/javascript"></script>
<script type="text/javascript">
new LoginBox('CH_boxLogin',1);</script>
</head>
<body>
<div id="CH_header">
	<a href="/"><img src="/img/cpp-logo.png" alt="cplusplus.com" width="198" height="75"></a>
	<div id="CH_bar">
		<b><a href="/">C++</a> : <a href="/reference/">Reference</a> : <a href="/reference/clibrary/">C Library</a> : <a href="/reference/clibrary/cstdio/">cstdio (stdio.h)</a> : printf</b>
	</div>
	
	<div id="CH_searchbox">
		<form id="search" action="/search.do" method="get">
		Search: <input name="q" size="13" class="txt"> <input type="submit" value="Search" class="btn">
		</form>
	</div>
	<div id="CH_boxLogin"><div class="loginmessage">&nbsp;<img src="/img/waitblue.gif"></div>
	</div>
</div>
<div id="CH_container">
<table id="CH_containertbl"><tr>
<td id="CH_nav">
<table class="box">
<thead>
<tr><th><a href="/">cplusplus.com</a></th></tr>
</thead>
<tbody>
<tr><td class="folder"><a href="/info/">Information</a></td></tr>
<tr><td class="folder"><a href="/doc/">Documentation</a></td></tr>
<tr><td class="folder selected"><a href="/reference/">Reference</a></td></tr>
<tr><td class="folder"><a href="/articles/">Articles</a></td></tr>
<tr><td class="folder"><a href="/src/">Sources</a></td></tr>
<tr><td class="folder"><a href="/forum/">Forum</a></td></tr>
</tbody>
</table>
<table class="box">
<thead><tr><th><a href="/reference/">Reference</a></th></tr></thead>
<tbody><tr><td class="selected folder"><a href="/reference/clibrary/">C Library</a></td></tr>
<tr><td class="folder"><a href="/reference/iostream/">IOstream Library</a></td></tr>
<tr><td class="folder"><a href="/reference/string/">Strings library</a></td></tr>
<tr><td class="folder"><a href="/reference/stl/">STL Containers</a></td></tr>
<tr><td class="folder"><a href="/reference/algorithm/">STL Algorithms</a></td></tr>
<tr><td class="folder"><a href="/reference/std/">Miscellaneous</a></td></tr>
</tbody>
</table>
<table class="box">
<thead><tr><th><a href="/reference/clibrary/">C Library</a></th></tr></thead>
<tbody><tr><td class="folder"><a href="/reference/clibrary/cassert/">cassert (assert.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cctype/">cctype (ctype.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cerrno/">cerrno (errno.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cfloat/">cfloat (float.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/ciso646/">ciso646 (iso646.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/climits/">climits (limits.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/clocale/">clocale (locale.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cmath/">cmath (math.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/csetjmp/">csetjmp (setjmp.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/csignal/">csignal (signal.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cstdarg/">cstdarg (stdarg.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cstddef/">cstddef (stddef.h)</a></td></tr>
<tr><td class="selected folder"><a href="/reference/clibrary/cstdio/">cstdio (stdio.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cstdlib/">cstdlib (stdlib.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/cstring/">cstring (string.h)</a></td></tr>
<tr><td class="folder"><a href="/reference/clibrary/ctime/">ctime (time.h)</a></td></tr>
</tbody>
</table>
<table class="box">
<thead><tr><th><a href="/reference/clibrary/cstdio/">cstdio (stdio.h)</a></th></tr></thead>
<tbody><tr><td class="folder">functions:</td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/clearerr/">clearerr</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fclose/">fclose</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/feof/">feof</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/ferror/">ferror</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fflush/">fflush</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fgetc/">fgetc</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fgetpos/">fgetpos</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fgets/">fgets</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fopen/">fopen</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fprintf/">fprintf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fputc/">fputc</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fputs/">fputs</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fread/">fread</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/freopen/">freopen</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fscanf/">fscanf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fseek/">fseek</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fsetpos/">fsetpos</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/ftell/">ftell</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fwrite/">fwrite</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/getc/">getc</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/getchar/">getchar</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/gets/">gets</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/perror/">perror</a></td></tr>
<tr><td class="selected indent"><a href="/reference/clibrary/cstdio/printf/">printf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/putc/">putc</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/putchar/">putchar</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/puts/">puts</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/remove/">remove</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/rename/">rename</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/rewind/">rewind</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/scanf/">scanf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/setbuf/">setbuf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/setvbuf/">setvbuf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/sprintf/">sprintf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/sscanf/">sscanf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/tmpfile/">tmpfile</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/tmpnam/">tmpnam</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/ungetc/">ungetc</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/vfprintf/">vfprintf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/vprintf/">vprintf</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/vsprintf/">vsprintf</a></td></tr>
<tr><td class="folder">macro constants:</td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/EOF/">EOF</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/FILENAME_MAX/">FILENAME_MAX</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/NULL/">NULL</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/TMP_MAX/">TMP_MAX</a></td></tr>
<tr><td class="folder">objects:</td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/stderr/">stderr</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/stdin/">stdin</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/stdout/">stdout</a></td></tr>
<tr><td class="folder">types:</td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/FILE/">FILE</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/fpos_t/">fpos_t</a></td></tr>
<tr><td class="indent"><a href="/reference/clibrary/cstdio/size_t/">size_t</a></td></tr>
</tbody>
</table>
<div id="CH_subnav"></div>
<br>
<script type="text/javascript"><!--
google_ad_client = "pub-7688470879129516";
/* single square */
google_ad_slot = "7465536459";
google_ad_width = 125;
google_ad_height = 125;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
	</td>
	<td id="CH_main">
<script type="text/javascript"><!--
google_ad_client = "pub-7688470879129516";
google_ad_slot = "7973859818";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br><div class="C_doctop">
<h1>printf</h1>
<div class="right">function</div>
</div>
<div class="docsubtop">
<div class="right"><code>&lt;cstdio&gt;</code></div>
<div class="prototype"><pre>int printf ( const char * format, ... );</pre></div>
</div>
<p><strong>Print formatted data to stdout</strong></p>
Writes to the standard output (stdout) a sequence of data formatted as the <i>format</i> argument specifies. After the <i>format</i> parameter, the function expects at least as many additional arguments as specified in <i>format</i>.<br>
<br>
<h3>Parameters</h3>
<dl>
<dt>format</dt>
<dd>String that contains the text to be written to <tt>stdout</tt>.<br>
It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested.<br>
The number of arguments following the <i>format</i> parameters should at least be as much as the number of format tags.<br>
The format tags follow this prototype:<br>
 <br>
<tt>%[flags][width][.precision][length]specifier</tt>
 <br>
Where <i>specifier</i> is the most significant one and defines the type and the interpretation of the value of the coresponding argument:<br>
<table class="boxed"><tr><th><i>specifier</i></th><th>Output</th><th>Example</th></tr>
<tr><td><tt>c</tt></td><td>Character</td><td><tt>a</tt></td></tr>
<tr><td><tt>d</tt> or <tt>i</tt></td><td>Signed decimal integer</td><td><tt>392</tt></td></tr>
<tr><td><tt>e</tt></td><td>Scientific notation (mantise/exponent) using <tt>e</tt> character</td><td><tt>3.9265e+2</tt></td></tr>
<tr><td><tt>E</tt></td><td>Scientific notation (mantise/exponent) using <tt>E</tt> character</td><td><tt>3.9265E+2</tt></td></tr>
<tr><td><tt>f</tt></td><td>Decimal floating point</td><td><tt>392.65</tt></td></tr>
<tr><td><tt>g</tt></td><td>Use the shorter of <tt>%e</tt> or <tt>%f</tt></td><td><tt>392.65</tt></td></tr>
<tr><td><tt>G</tt></td><td>Use the shorter of <tt>%E</tt> or <tt>%f</tt></td><td><tt>392.65</tt></td></tr>
<tr><td><tt>o</tt></td><td>Unsigned octal</td><td><tt>610</tt></td></tr>
<tr><td><tt>s</tt></td><td>String of characters</td><td><tt>sample</tt></td></tr>
<tr><td><tt>u</tt></td><td>Unsigned decimal integer</td><td><tt>7235</tt></td></tr>
<tr><td><tt>x</tt></td><td>Unsigned hexadecimal integer</td><td><tt>7fa</tt></td></tr>
<tr><td><tt>X</tt></td><td>Unsigned hexadecimal integer (capital letters)</td><td><tt>7FA</tt></td></tr>
<tr><td><tt>p</tt></td><td>Pointer address</td><td><tt>B800:0000</tt></td></tr>
<tr><td><tt>n</tt></td><td>Nothing printed. The argument must be a pointer to a signed <tt>int</tt>, where the number of characters written so far is stored.</td><td>  </td></tr>
<tr><td><tt>%</tt></td><td>A <tt>%</tt> followed by another <tt>%</tt> character will write <tt>%</tt> to <tt>stdout</tt>.</td><td><tt>%</tt></td></tr>
</table>
<br>
The tag can also contain <i>flags</i>, <i>width</i>, <i>.precision</i> and <i>modifiers</i> sub-specifiers, which are optional and follow these specifications:<br>
 <br>
<table class="boxed"><tr><th><i>flags</i></th><th>description</th></tr>
<tr><td><tt>-</tt></td><td>Left-justify within the given field width; Right justification is the default (see <i>width</i> sub-specifier).</td></tr>
<tr><td><tt>+</tt></td><td>Forces to precede the result with a plus or minus sign (<tt>+</tt> or <tt>-</tt>) even for positive numbers. By default, only negative numbers are preceded with a <tt>-</tt> sign.</td></tr>
<tr><td><i>(space)</i></td><td>If no sign is going to be written, a blank space is inserted before the value.</td></tr>
<tr><td><tt>#</tt></td><td>Used with <tt>o</tt>, <tt>x</tt> or <tt>X</tt> specifiers the value is preceeded with <tt>0</tt>, <tt>0x</tt> or <tt>0X</tt> respectively for values different than zero.<br>
Used with <tt>e</tt>, <tt>E</tt> and <tt>f</tt>, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.<br>
Used with <tt>g</tt> or <tt>G</tt> the result is the same as with <tt>e</tt> or <tt>E</tt> but trailing zeros are not removed.</td></tr>
<tr><td><tt>0</tt></td><td>Left-pads the number with zeroes (<tt>0</tt>) instead of spaces, where padding is specified (see <i>width</i> sub-specifier).</td></tr>
</table>
<br>
<table class="boxed"><tr><th><i>width</i></th><th>description</th></tr>
<tr><td><i>(number)</i></td><td>Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.</td></tr>
<tr><td><tt>*</tt></td><td>The <i>width</i> is not specified in the <i>format</i> string, but as an additional integer value argument preceding the argument that has to be formatted.</td></tr></table>
 <br>
<table class="boxed"><tr><th><i>.precision</i></th><th>description</th></tr>
<tr><td><tt>.</tt><i>number</i></td><td>For integer specifiers (<tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, <tt>X</tt>): <i>precision</i> specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A <i>precision</i> of <tt>0</tt> means that no character is written for the value <tt>0</tt>.<br>
For <tt>e</tt>, <tt>E</tt> and <tt>f</tt> specifiers: this is the number of digits to be printed <strong>after</strong> the decimal point.<br>
For <tt>g</tt> and <tt>G</tt> specifiers: This is the maximum number of significant digits to be printed.<br>
For <tt>s</tt>: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.<br>
For <tt>c</tt> type: it has no effect.<br>
When no <i>precision</i> is specified, the default is <tt>1</tt>. If the period is specified without an explicit value for <i>precision</i>, <tt>0</tt> is assumed.<br>
</td></tr>
<tr><td><tt>.*</tt></td><td>The <i>precision</i> is not specified in the <i>format</i> string, but as an additional integer value argument preceding the argument that has to be formatted.</td></tr></table>
<br>
<table class="boxed"><tr><th><i>length</i></th><th>description</th></tr>
<tr><td><tt>h</tt></td><td>The argument is interpreted as a <tt>short int</tt> or <tt>unsigned short int</tt> (only applies to integer specifiers: <tt>i</tt>, <tt>d</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt> and <tt>X</tt>).</td></tr>
<tr><td><tt>l</tt></td><td>The argument is interpreted as a <tt>long int</tt> or <tt>unsigned long int</tt> for integer specifiers (<tt>i</tt>, <tt>d</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt> and <tt>X</tt>), and as a wide character or wide character string for specifiers <tt>c</tt> and <tt>s</tt>.</td></tr>
<tr><td><tt>L</tt></td><td>The argument is interpreted as a <tt>long double</tt> (only applies to floating point specifiers: <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>g</tt> and <tt>G</tt>). </td></tr></table></dd>
<br>
<dt>additional arguments</dt>
<dd>Depending on the <i>format</i> string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each <tt>%</tt>-tag specified in the <i>format</i> parameter, if any. There should be the same number of these arguments as the number of <tt>%</tt>-tags that expect a value.</dd>
</dl>
<br>
<h3>Return Value</h3>
On success, the total number of characters written is returned.<br>
On failure, a negative number is returned.<br>
<br>
<h3>Example</h3>
<div class="auto"><table class="snippet"><tr><td class="rownum"><tt>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br></tt></td>
<td class="source"><pre><code><cite>/* printf example */</cite>
<dfn>#include &lt;stdio.h&gt;</dfn>

<var>int</var> main()
{
   printf (<kbd>"Characters: %c %c \n"</kbd>, <kbd>'a'</kbd>, 65);
   printf (<kbd>"Decimals: %d %ld\n"</kbd>, 1977, 650000L);
   printf (<kbd>"Preceding with blanks: %10d \n"</kbd>, 1977);
   printf (<kbd>"Preceding with zeros: %010d \n"</kbd>, 1977);
   printf (<kbd>"Some different radixes: %d %x %o %#x %#o \n"</kbd>, 100, 100, 100, 100, 100);
   printf (<kbd>"floats: %4.2f %+.0e %E \n"</kbd>, 3.1416, 3.1416, 3.1416);
   printf (<kbd>"Width trick: %*d \n"</kbd>, 5, 10);
   printf (<kbd>"%s \n"</kbd>, <kbd>"A string"</kbd>);
   <var>return</var> 0;
}</code></pre></td></tr></table></div>
<br>
<br>
And here is the output:<br>
<br>
<tt><pre >
Characters: a A
Decimals: 1977 650000
Preceding with blanks:       1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick:    10
A string
</pre></tt>
<br>
<h3>See also</h3>
<table class="C_docLink"><tr><td class="tit"><a href= "/reference/clibrary/cstdio/puts/"><b>puts</b></a></td><td class="des"> Write string to stdout <span class="typ">(function)</span></td></tr></table><table class="C_docLink"><tr><td class="tit"><a href= "/reference/clibrary/cstdio/scanf/"><b>scanf</b></a></td><td class="des"> Read formatted data from stdin <span class="typ">(function)</span></td></tr></table><table class="C_docLink"><tr><td class="tit"><a href= "/reference/clibrary/cstdio/fprintf/"><b>fprintf</b></a></td><td class="des"> Write formatted output to stream <span class="typ">(function)</span></td></tr></table><table class="C_docLink"><tr><td class="tit"><a href= "/reference/clibrary/cstdio/fwrite/"><b>fwrite</b></a></td><td class="des"> Write block of data to stream <span class="typ">(function)</span></td></tr></table>
<div id="CH_bb"></div><script type="text/javascript">new doc_Page('CH_bb','/reference/item.cgi',87,0,0,32768);</script>
<p>


</td></tr></table>
</div>
<div id="CH_footer">
	<a href="/">Home page</a> | <a href="/privacy.do">Privacy policy</a><br>&copy; cplusplus.com, 2000-2011 - All rights reserved - <i>v2.9</i><br><a href="/contact.do?referrer=http://www.cplusplus.com/reference/clibrary/cstdio/printf/">Spotted an error? contact us</a>
</div>

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
onPrint.print();
var gso = new Session ('ssn29','rel29',14400,86400,1301118369);
_uacct = "UA-521783-1";
urchinTracker();
</script>
</body>
</html>